
package com.changjiang.luban.config.resolver;


import com.changjiang.luban.config.annotation.CurrentUser;
import com.changjiang.luban.model.AuthorizationUser;
import com.changjiang.luban.utils.SystemConstants;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 描述:将含有 CurrentUser 注解的方法参数注入当前登录用户
 * @author nichangjiang@vip.qq.com
 * 创建时间: 2017/12/23. 21:39
 * 修改人:
 * 修改时间:
 * 修改备注:
 * 版本:v1.0
 */
@Component
public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(AuthorizationUser.class) && parameter.hasParameterAnnotation (CurrentUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        Long userCode = (Long) webRequest.getAttribute(SystemConstants.CURRENT_USER_CODE, RequestAttributes.SCOPE_REQUEST);
        return new AuthorizationUser(userCode);
    }
}
